//
//  CDSetSwitchView.m
//  CuriosityDaily3
//
//  Created by donghao on 16/9/24.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDSetSwitchView.h"

@interface CDSetSwitchView ()

@property(nonatomic,weak) UIView *circularView;


@end

@implementation CDSetSwitchView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

/**
 *  设置界面
 */
- (void)setupUI {
    UIView *circularView = [UIView new];
    circularView.layer.cornerRadius = 7;
    [self addSubview:circularView];
    [circularView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(-1);
        make.size.mas_equalTo(CGSizeMake(12, 12));
        make.left.equalTo(self).offset(4);
    }];
    
    self.circularView = circularView;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code

        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(1, 1, rect.size.width - 2, rect.size.height - 2) cornerRadius:(rect.size.width - 4)* 0.5];
        [self.tintColor setStroke];
        [path stroke];
    
}

- (void)setSwitchState:(CDSwitchState)switchState {
    _switchState = switchState;
    if (_switchState == CDSwitchStateNormal) {
        self.tintColor = [UIColor whiteColor];
        [self.circularView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(4);
        }];
        if ([self.delegate respondsToSelector:@selector(switchView:stateChanged:)]) {
            [self.delegate switchView:self stateChanged:switchState];
        }
        
    } else if (_switchState == CDSwitchStateSelected) {
        self.tintColor = [UIColor orangeColor];
        [self.circularView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20);
        }];
        if ([self.delegate respondsToSelector:@selector(switchView:stateChanged:)]) {
            [self.delegate switchView:self stateChanged:switchState];
        }
    }
    self.circularView.backgroundColor = self.tintColor;
    [self layoutIfNeeded];
    [self setNeedsDisplay];
    
}

- (UIColor *)tintColor {
    if (_tintColor == nil) {
        _tintColor = [UIColor cyanColor];
    }
    return _tintColor;
    
}

@end
